package com.example.a28796.beautiful;

import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.a28796.beautiful.config.NetConfig;
import com.example.a28796.beautiful.zhangyuqiadapter.BeautyMoreAdapter;
import com.example.a28796.beautiful.zhangyuqibean.MoreBean;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MoreActivity extends AppCompatActivity {
    private BaseAdapter mAdapter;
    private ListView mListView;
    private List<MoreBean.ObjBean.ListDataBean> mData;
    private ActionBar actionBar;
    private ImageView mImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        actionBar = getSupportActionBar();
        actionBar.hide();
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {


        mData = new ArrayList<>();
        mAdapter = new BeautyMoreAdapter(this, mData);
        x.http().get(new RequestParams(NetConfig.MORE_PATH), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MoreBean moreBean = gson.fromJson(result, MoreBean.class);
                List<MoreBean.ObjBean.ListDataBean> listData = moreBean.getObj().getListData();
                for (int i = 0; i < listData.size(); i++) {
                    mData.add(listData.get(i));
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.lv_more_show);
        mImageView = (ImageView) findViewById(R.id.iv_more_back);
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.bt_more_local:
                    ArrayList<String> mainMenuItems = new ArrayList<>(Arrays.asList("全国", "北京", "重庆", "福建", "广东", "海南", "河北", "黑龙江", "河南", "湖北"
                            , "湖南"
                    ));
                    String[][] sub_main_menuArr = new String[][]{{"北京"}, {"重庆"}, {"福建省", "福州"}, {"广东省", "广州", "东莞", "佛山", "深圳"}, {"海南省", "海口", "三亚"}
                            , {"河北省", "石家庄"}, {"黑龙江省", "哈尔滨"}, {"河南省", "郑州"}, {"湖北省", "荆州", "武汉", "咸宁", "宜昌"}
                            , {"湖南省", "长沙"}
                    };

                    ArrayList<String> subMenueList = new ArrayList<>();
                    showMainMenu(view, mainMenuItems, sub_main_menuArr, subMenueList);
                    break;
                case R.id.bt_more_all:

                    break;
                case R.id.bt_more_zhineng:
                    ArrayList<String> zhiNengPopItems = new ArrayList<>(Arrays.asList("智能排序", "价格最低", "销量最高"));
                    ArrayList<String> zhiNengPopList = new ArrayList<>();

                    showZhinengMenu(view,zhiNengPopItems,zhiNengPopList);
                    break;
                case R.id.bt_more_shaixuan:

                    break;
                default:
                    break;
            }
        }
    }


    private void showMainMenu(final View view, final ArrayList<String> mainMenuItems, final String[][] sub_main_menuArr, final ArrayList<String> subMenueList) {

        View contentView = LayoutInflater.from(this).inflate(
                R.layout.view_pop_menu, null);

        final ListView mainMeneLV = (ListView) contentView.findViewById(R.id.menu_listview);
        ArrayAdapter<String> mainMenuAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mainMenuItems);

        mainMeneLV.setAdapter(mainMenuAdapter);


        final ListView subMeneLV = (ListView) contentView.findViewById(R.id.submenu_listview);
        final ArrayAdapter<String> subMenuAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, subMenueList);
        subMeneLV.setAdapter(subMenuAdapter);

        mainMeneLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position == 0) {
                    subMeneLV.setVisibility(View.INVISIBLE);
                } else {

                    subMeneLV.setVisibility(View.VISIBLE);
                    subMenueList.clear();
                    for (int i = 0; i < mainMenuItems.size(); i++) {
                        mainMeneLV.getChildAt(i).setBackgroundColor(Color.GRAY);
                    }
                    mainMeneLV.getChildAt(position).setBackgroundColor(Color.WHITE);
                    int lenth = sub_main_menuArr[position - 1].length;
                    for (int i = 0; i < lenth; i++) {
                        subMenueList.add(sub_main_menuArr[position - 1][i]);

                    }
                    subMenuAdapter.notifyDataSetChanged();
                }
            }
        });

        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        subMeneLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MoreActivity.this, "您选择了" + subMenueList.get(position), Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(view);

    }

    private void showZhinengMenu(final View view,ArrayList<String> zhiNengPopItems, ArrayList<String> zhiNengPopList) {
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.view_pop_zhineng, null);

        final ListView mainMeneLV = (ListView) contentView.findViewById(R.id.view_pop_zhineng);
        ArrayAdapter<String> mainMenuAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, zhiNengPopItems);

        mainMeneLV.setAdapter(mainMenuAdapter);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        mainMeneLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) {
                    Toast.makeText(MoreActivity.this, "智能排序", Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                }else if(position == 1) {
                    Toast.makeText(MoreActivity.this, "价格最低", Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                }else{
                    Toast.makeText(MoreActivity.this, "销量最高", Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                }
            }
        });
        popupWindow.showAsDropDown(view);
    }
}

